package com.hejian.android.a72ggametools.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.activity.GameDetailActivity;
import com.hejian.android.a72ggametools.activity.LoginActivity;
import com.hejian.android.a72ggametools.activity.TryPlayActivity;
import com.hejian.android.a72ggametools.activity.UserInfoActivity;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.net.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/13.
 */
public class MineFragment extends Fragment implements View.OnClickListener {
    private View view;
    private Button myMessageBtn;
    private LinearLayout mineLogin;
    private Button mineGiftBtn;
    private Button mineActionBtn;
    private Button mineConvertBtn;
    private LinearLayout mineMyTask;
    private LinearLayout mineMscRedBag;
    private LinearLayout mineFindFriend;
    private Button mineTryPlayBtn;
    private Bundle bundle;
    private String nickName;
    private Intent msgIntent;
    private ImageCache imageCache = new DoubleCache();
    private ImageLoader imageLoader;
    private ImageView mineGameImg;
    private LinearLayout mineGameLL;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine, null);
        imageLoader = ImageLoader.getInstance(getActivity(),imageCache);
        bundle = getArguments();
        if (bundle != null) {
            nickName = bundle.getString("nickname");
        }
        initView();
        initData();
        setListener();
        return view;
    }

    private void initData() {
        initFootData();
    }

    private void initFootData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadFootData();
            }
        }).start();
    }

    private void loadFootData() {
        String imgUrl= null;
        Map<String,String> map = new HashMap<String,String>();
        map.put("platform","2");
        map.put("pos","4");
        String buffer = HttpUtils.doPostRequest(Constant.MINE_GAME_URL,map);
        try {
            JSONObject jsonObject = new JSONObject(buffer);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            imgUrl = jsonArray.getJSONObject(0).getString("bimg");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        imageLoader.displayImage(imgUrl,mineGameImg);


    }

    private void setListener() {
        myMessageBtn.setOnClickListener(this);
        mineLogin.setOnClickListener(this);
        mineGiftBtn.setOnClickListener(this);
        mineActionBtn.setOnClickListener(this);
        mineConvertBtn.setOnClickListener(this);
        mineMyTask.setOnClickListener(this);
        mineMscRedBag.setOnClickListener(this);
        mineFindFriend.setOnClickListener(this);
        mineTryPlayBtn.setOnClickListener(this);
        mineGameLL.setOnClickListener(this);
    }

    private void initView() {
        myMessageBtn = (Button) view.findViewById(R.id.mine_message_btn);
        mineLogin = (LinearLayout) view.findViewById(R.id.mine_login_ll);
        if (nickName != null) {
            ((TextView) mineLogin.getChildAt(0)).setText(nickName);
        }
        mineGiftBtn = (Button) view.findViewById(R.id.mine_gift_btn);
        mineActionBtn = (Button) view.findViewById(R.id.mine_action_btn);
        mineConvertBtn = (Button) view.findViewById(R.id.mine_convert_btn);
        mineTryPlayBtn = (Button) view.findViewById(R.id.mine_try_play_btn);
        mineMyTask = (LinearLayout) view.findViewById(R.id.my_task);
        mineMscRedBag = (LinearLayout) view.findViewById(R.id.get_money_from_micromsg);
        mineFindFriend = (LinearLayout) view.findViewById(R.id.find_friend);
        mineGameImg = (ImageView)view.findViewById(R.id.mine_game_img);
        mineGameLL = (LinearLayout)view.findViewById(R.id.ll_mine_game);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mine_login_ll:
                if (nickName != null) {
                    msgIntent = new Intent(getActivity(), UserInfoActivity.class);
                    msgIntent.putExtra("userbundle",bundle);
                    startActivity(msgIntent);
                } else {
                    msgIntent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(msgIntent);
                }
                break;
            case R.id.mine_gift_btn:
            case R.id.mine_action_btn:
            case R.id.mine_convert_btn:
            case R.id.mine_message_btn:
            case R.id.my_task:
            case R.id.get_money_from_micromsg:
            case R.id.find_friend:
                msgIntent = new Intent(getActivity(), LoginActivity.class);
                startActivity(msgIntent);
                break;
            case R.id.mine_try_play_btn:
                Intent intent = new Intent();
                intent.setClass(getActivity(), TryPlayActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_mine_game:
                Intent detailIntent = new Intent(getActivity(), GameDetailActivity.class);
                detailIntent.putExtra("id","1");
                startActivity(detailIntent);
                break;

        }
    }
}
